package com.cjjd.c.base.recycler;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * ----------------------------------
 * Created by xyd on 2017/4/19.
 * <p/>
 * 描  述：
 * <p>
 * 三个问题：
 * 1. 数据怎么办？   泛型
 * 2. 布局怎么办？   构造
 * 3. 绑定怎么办？   回传
 * <p>
 * <p>
 * <p/>
 * 修订历史:
 * ----------------------------------
 */
public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected Context mContext;
    protected LayoutInflater mInflater;
    protected List<T> mDataList;
    private int mLayoutId;

    public RecyclerAdapter(Context context, List<T> dataList, int layoutId) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDataList = dataList;
        this.mLayoutId = layoutId;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(mLayoutId, parent, false);
        ViewHolder holder = new ViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        convert((ViewHolder) holder, mDataList.get(position),position);
    }


    /**
     * 利用抽象方法回传出去，每个不一样的Adapter去设置
     *
     * @param itemData 当前的数据
     */
    public abstract void convert(ViewHolder holder, T itemData, int position);

    @Override
    public int getItemCount() {
        //Log.e("xyd", "数据个数是：" + mDataList.size());
        return mDataList.size();
    }


}
